package com.zjhome.cloudnote.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import com.zjhome.cloudnote.domain.DirInfo;
import com.zjhome.cloudnote.domain.NoteInfo;
import com.zjhome.cloudnote.domain.RecycleInfo;
import com.zjhome.cloudnote.domain.ResourceInfo;
import com.zjhome.cloudnote.service.DirService;
import com.zjhome.cloudnote.service.ExplorerService;
import com.zjhome.cloudnote.service.NoteService;
import com.zjhome.cloudnote.service.RecycleService;
import com.zjhome.cloudnote.util.ExplorerUtil;

@Service
public class ExplorerServiceImpl implements ExplorerService {
	/**
	 * 文件夹服务
	 */
	@Autowired
	private DirService		dirService;
	
	/**
	 * 笔记服务
	 */
	@Autowired
	private NoteService		noteService;
	
	/**
	 * 回收站服务
	 */
	@Autowired
	private RecycleService	recycleService;
	

	@Override
	public List<ResourceInfo> selectSubDirInfo(String did, String name, String uid) throws Exception {
		// 查询子文件夹
		// 构造查询参数
		DirInfo dirInfo = new DirInfo();
		
		dirInfo.setName(name);
		dirInfo.setPid(did);
		dirInfo.setUid(uid);
		dirInfo.setDelflag(0);	// 未被删除
		
		List<DirInfo> dirList = dirService.selectDirsByParam(dirInfo);		
		
		// 查询子文件
		// 构造查询参数
		NoteInfo noteInfo = new NoteInfo();
		
		noteInfo.setDid(did);
		noteInfo.setTitle(name);
		noteInfo.setUid(uid);
		noteInfo.setDelflag(0);	// 未被删除
		
		List<NoteInfo> noteList = noteService.selectNotesByParam(noteInfo);
		
		// 转换为 ResourceInfo
		List<ResourceInfo> resList = new ArrayList<>();
		
		for (DirInfo dir: dirList) {
			resList.add(dir.toResourceInfo());
		}
		for (NoteInfo note: noteList) {
			resList.add(note.toResourceInfo());
		}
		
		return resList;
	}


	@Override
	public int updateResource(ResourceInfo resourceInfo, String uid) throws Exception {
		//
		int ret = 0;
		if ("dir".equals(resourceInfo.getType())) {
			// 修改文件夹 名称
			DirInfo dirInfo = resourceInfo.toDirInfo();
			dirInfo.setUid(uid);
			
			ret = dirService.updateDirByParam(dirInfo);
		}
		else {
			// 修改笔记名称
			NoteInfo noteInfo = resourceInfo.toNoteInfo();
			noteInfo.setUid(uid);
			
			ret = noteService.updateNoteByParam(noteInfo);			
		}
		return ret;
	}


	@Override
	public int deleteResource(ResourceInfo resourceInfo, String uid) throws Exception {		
		// 更新资源状态
		int ret = 0;
		if (ExplorerUtil.judgeResType(resourceInfo.getType()) == ExplorerUtil.RESTYPE_DIR) {
			// 文件夹
			//			
			dirService.updateDirDelFlagByDir(resourceInfo.getId(), uid, 1);		// 置位所有的子文件夹的删除标志
			noteService.updateNoteDelFlagByDir(resourceInfo.getId(), uid, 1);	// 置位所有的子笔记的删除标志
						
			// 置位本文件夹的删除标志
			DirInfo dirInfo = new DirInfo();
			
			dirInfo.setId(resourceInfo.getId());	//资源id
			dirInfo.setDelflag(1);	// 置位删除标志
			dirInfo.setUid(uid);
			dirService.updateDirByParam(dirInfo);	
		}
		else {			
			// 删除笔记
			NoteInfo noteInfo = new NoteInfo();
			
			noteInfo.setId(resourceInfo.getId());	// 资源id
			noteInfo.setDelflag(1);	// 删除标志
			noteInfo.setUid(uid);
			ret = noteService.updateNoteByParam(noteInfo);			
		}
		
		// 在回收站中建立条目数据
		RecycleInfo recycleInfo = new RecycleInfo();
		
		recycleInfo.setResid(resourceInfo.getId());
		recycleInfo.setResname(resourceInfo.getName());
		recycleInfo.setRestype(resourceInfo.getType());
		recycleInfo.setUid(uid);
		ret = recycleService.insertRecycleByParam(recycleInfo);

		return 0;
	}

}
